package com.fyzk.dialog;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.PopupWindow;

import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.chongzhi.epay.Util;
import com.tencent.connect.share.QQShare;
import com.tencent.mm.opensdk.modelmsg.SendMessageToWX;
import com.tencent.mm.opensdk.modelmsg.WXImageObject;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.modelmsg.WXWebpageObject;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.tencent.tauth.IUiListener;
import com.tencent.tauth.Tencent;
import com.tencent.tauth.UiError;
import com.tyjc.lottery.R;

public class SharePop extends PopupWindow {

    private ImageButton share_qq, share_wx, share_pq, share_more;

    private Activity context;
    private IWXAPI api;

    private View full_view;

    public SharePop(Activity context, int width, int height) {
        super(width, height);
        View view = View.inflate(context, R.layout.layout_selshare, null);
        this.setContentView(view);
        initView(view);
        this.setOutsideTouchable(true);
        this.setBackgroundDrawable(context.getResources().getDrawable(R.color.transparent));
        // 设置此参数获得焦点，否则无法点击
        this.setFocusable(true);
        this.context = context;
        api = WXAPIFactory.createWXAPI(context, "wxef8b1916114352a7");
    }

    private void initView(View view) {
        share_pq = view.findViewById(R.id.share_pq);
        share_qq = view.findViewById(R.id.share_qq);
        share_wx = view.findViewById(R.id.share_wx);
        full_view = view.findViewById(R.id.full_view);
        share_more = view.findViewById(R.id.share_more);
        share_pq.setOnClickListener(onClickListener);
        share_qq.setOnClickListener(onClickListener);
        share_wx.setOnClickListener(onClickListener);
        share_more.setOnClickListener(onClickListener);
        full_view.setOnClickListener(onClickListener);

    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.share_pq:
                    shareToPQ();
                    break;
                case R.id.share_qq:
                    shareToQQ();
                    break;
                case R.id.share_more:
                    shareMore();
                    break;
                case R.id.share_wx:
                    shareToWX();
                 break;
                case R.id.full_view:
                    SharePop.this.dismiss();
                 break;
            }
            SharePop.this.dismiss();
        }
    };

    private void shareMore() {
        Intent intent1 = new Intent(Intent.ACTION_SEND);
        intent1.putExtra(Intent.EXTRA_TEXT, "http://a.app.qq.com/o/simple.jsp?pkgname=com.fyzc.lcc");
        intent1.setType("text/plain");
        context.startActivity(Intent.createChooser(intent1, "分享乐彩竞猜给朋友"));
    }

    private void shareToQQ() {
        Tencent mTencent = Tencent.createInstance("1105928181", context);
        final Bundle params = new Bundle();
        params.putInt(QQShare.SHARE_TO_QQ_KEY_TYPE, QQShare.SHARE_TO_QQ_TYPE_DEFAULT);
        params.putString(QQShare.SHARE_TO_QQ_TITLE, "乐彩竞猜");
        params.putString(QQShare.SHARE_TO_QQ_SUMMARY, "乐彩竞猜，简单可信赖的购彩平台");
//                params.putString(QQShare.SHARE_TO_QQ_TARGET_URL,  "http://www.idoucx.com/lotteryWeb/sharewx.html");
        params.putString(QQShare.SHARE_TO_QQ_TARGET_URL, "http://a.app.qq.com/o/simple.jsp?pkgname=com.fyzc.lcc");
        params.putString(QQShare.SHARE_TO_QQ_IMAGE_URL, "http://imgcache.qq.com/qzone/space_item/pre/0/66768.gif");
        params.putString(QQShare.SHARE_TO_QQ_APP_NAME, "乐彩竞猜");
//                params.putInt(QQShare.SHARE_TO_QQ_EXT_INT,  "其他附加功能");
        mTencent.shareToQQ(context, params, new IUiListener() {

            @Override
            public void onComplete(Object o) {
                AppToast.getToast().show("分享成功");
            }

            @Override
            public void onError(UiError uiError) {
                AppToast.getToast().show("分享失败");
            }

            @Override
            public void onCancel() {
                AppToast.getToast().show("取消分享");
            }
        });
    }

    private void shareToWX() {
        WXWebpageObject webpageObject = new WXWebpageObject();
        webpageObject.webpageUrl = "http://a.app.qq.com/o/simple.jsp?pkgname=com.fyzc.lcc";


        WXMediaMessage msg = new WXMediaMessage(webpageObject);

        msg.title = "乐彩竞猜";

        msg.description = "乐彩竞猜，简单可信赖的购彩平台";

        Bitmap thumb = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
//        Bitmap thumbBmp = Bitmap.createScaledBitmap(thumb, 200, 200, true);
//        thumb.recycle();
        msg.thumbData = Util.bmpToByteArray(thumb, true);


        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = "webpage";
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneSession;
        api.sendReq(req);
    }

    private void shareToPQ() {

        Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.share);

        WXImageObject wxImageObject = new WXImageObject(bitmap);
        WXMediaMessage msg = new WXMediaMessage();
        msg.mediaObject = wxImageObject;
        msg.title = "乐彩竞猜";

        msg.description = "乐彩竞猜，简单可信赖的购彩平台";

        Bitmap thumb = BitmapFactory.decodeResource(context.getResources(), R.drawable.share_th);
        msg.thumbData = Util.bmpToByteArray(thumb, true);


        SendMessageToWX.Req req = new SendMessageToWX.Req();
        req.transaction = "img";
        req.message = msg;
        req.scene = SendMessageToWX.Req.WXSceneTimeline;
        api.sendReq(req);
    }

    private String buildTransaction(final String type) {
        return (type == null) ? String.valueOf(System.currentTimeMillis()) : type + System
                .currentTimeMillis();
    }

    public void setOnWFCheckChangedListener(OnWFCheckChangedListener listener) {
        this.listener = listener;
    }


    private OnWFCheckChangedListener listener = null;

    public interface OnWFCheckChangedListener {
        void onWFCheckChange(int id, boolean isChecked);
    }

//    @Override
//    public void showAtLocation(View parent, int gravity, int x, int y) {
//        super.showAsDropDown(parent);
//    }


}